package gui;


import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;

import libraries.Library;
import metagrammar.Type;
import util.FowardingComboBoxModel;
import util.OperatorWrapper;
import application.Application;
import application.GlobalContext;

public class LibraryList extends JList<LibraryWrapper> implements OperatorModelProvider,
TypeModelProvider {
    
    public LibraryList()
    {
        DefaultComboBoxModel<LibraryWrapper> libraryModels = new DefaultComboBoxModel<LibraryWrapper>();
        setModel(libraryModels);
        
        libraryModels.addElement(new LibraryWrapper(Library.Known));
        
        GlobalContext context = Application.injector.getInstance(GlobalContext.class);
        Collection<Library> libraries = context.getAllLibraries();
        for(Library lib : libraries) {
            libraryModels.addElement(new LibraryWrapper(lib));
        }
        
        libraryModels.addElement(new LibraryWrapper(Library.Everything));
        
        this.setBorder(BorderFactory.createTitledBorder("Library"));
    }
    
    public OperatorComboBox createOperatorComboBox()
    {
        return new OperatorComboBox(this);
    }
    
    public OperatorList createOperatorList()
    {
        return new OperatorList(this);
    }
    
    public TypeComboBox createTypeComboBox()
    {
        return new TypeComboBox(this);
    }
    
    public TypeList createTypeList()
    {
        return new TypeList(this);
    }
    
    @Override
    public ComboBoxModel<Type> getTypeModel()
    {
        return new FowardingComboBoxModel<Type>(getSelectedValue().getTypeModel());
    }
    
    @Override
    public ComboBoxModel<OperatorWrapper> getOperatorModel()
    {
        return new FowardingComboBoxModel<OperatorWrapper>(getSelectedValue().getOperatorModel());
    }
}
